<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>svm1d.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>model</span>=<span class=defun_name>svm1d</span>(<span class=defun_in>data,options</span>)<br>
<span class=h1>%&nbsp;SVM1D&nbsp;Linear&nbsp;SVM&nbsp;for&nbsp;1-dimensional&nbsp;input&nbsp;data.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;svm1d(&nbsp;data&nbsp;)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;svm1d(&nbsp;data,&nbsp;options&nbsp;)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;svm1d(&nbsp;data&nbsp;)&nbsp;trains&nbsp;the&nbsp;linear&nbsp;SVM&nbsp;binary</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;classifier&nbsp;for&nbsp;the&nbsp;1-dimensional&nbsp;training&nbsp;data.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;The&nbsp;optimizer&nbsp;is&nbsp;based&nbsp;on&nbsp;a&nbsp;modification&nbsp;of&nbsp;the&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;Sequential&nbsp;Minimal&nbsp;Optimizer&nbsp;(SMO)&nbsp;[Platt98].&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;The&nbsp;trainined&nbsp;classfier&nbsp;is&nbsp;defined&nbsp;as</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q(x)&nbsp;=&nbsp;1&nbsp;if&nbsp;W*x&nbsp;+&nbsp;b&nbsp;&gt;=&nbsp;0</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;2&nbsp;if&nbsp;W*x&nbsp;+&nbsp;b&nbsp;&lt;&nbsp;0</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;svm1d(&nbsp;data,&nbsp;options&nbsp;)&nbsp;use&nbsp;to&nbsp;set&nbsp;up&nbsp;control</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;parameters&nbsp;for&nbsp;the&nbsp;SVM&nbsp;and&nbsp;the&nbsp;SMO&nbsp;algorithm.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;[struct]&nbsp;Input&nbsp;1-dimensional&nbsp;binary&nbsp;labeled&nbsp;training&nbsp;data:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.X&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Training&nbsp;numbers.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Labels&nbsp;(1&nbsp;or&nbsp;2).</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;[struct]&nbsp;Control&nbsp;parameters:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.C&nbsp;[1x1]&nbsp;SVM&nbsp;regularization&nbsp;constant&nbsp;(default&nbsp;C=inf).&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.eps&nbsp;[1x1]&nbsp;Tolerance&nbsp;of&nbsp;KKT-conditions&nbsp;(default&nbsp;eps=0.001).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.tol&nbsp;[1x1]&nbsp;Minimal&nbsp;change&nbsp;of&nbsp;variables&nbsp;(default&nbsp;tol=0.001).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Found&nbsp;SVM&nbsp;model:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Alpha&nbsp;[nsv&nbsp;x&nbsp;1]&nbsp;Weights.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.b&nbsp;[1x1]&nbsp;Bias&nbsp;of&nbsp;decision&nbsp;function.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.sv.X&nbsp;[1&nbsp;x&nbsp;nsv]&nbsp;Support&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.W&nbsp;[1x1]&nbsp;Explicit&nbsp;value&nbsp;of&nbsp;the&nbsp;normal&nbsp;vector&nbsp;(scalar).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.nsv&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;Support&nbsp;Vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.kercnt&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;kernel&nbsp;evaluations&nbsp;(multiplications&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in&nbsp;this&nbsp;1-d&nbsp;linear&nbsp;case)&nbsp;used&nbsp;by&nbsp;the&nbsp;SMO.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.trnerr&nbsp;[1x1]&nbsp;Training&nbsp;classification&nbsp;error.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.margin&nbsp;[1x1]&nbsp;Margin&nbsp;of&nbsp;found&nbsp;classifier.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.cputime&nbsp;[1x1]&nbsp;Used&nbsp;CPU&nbsp;time&nbsp;in&nbsp;seconds.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.options&nbsp;[struct]&nbsp;Copy&nbsp;of&nbsp;used&nbsp;options.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;SMO,&nbsp;SVMCLASS,&nbsp;KFD,&nbsp;KFDQP.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;17-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;14-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;15-july-2003,&nbsp;VF</span><br>
<br>
<hr>
<span class=comment>%&nbsp;timer</span><br>
tic;<br>
<br>
<span class=comment>%&nbsp;Process&nbsp;input&nbsp;arguments&nbsp;</span><br>
<span class=comment>%&nbsp;--------------------------</span><br>
[dim,num_data]&nbsp;=&nbsp;size(data.X);<br>
<span class=keyword>if</span>&nbsp;dim&nbsp;~=&nbsp;1,<br>
&nbsp;&nbsp;<span class=error>error</span>(<span class=quotes>'Inpu&nbsp;&nbsp;data&nbsp;must&nbsp;be&nbsp;one-dimensional.'</span>);<br>
<span class=keyword>end</span><br>
&nbsp;&nbsp;<br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;2,&nbsp;&nbsp;options&nbsp;=&nbsp;[];&nbsp;<span class=keyword>else</span>&nbsp;options=c2s(options);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'C'</span>),&nbsp;options.C&nbsp;=&nbsp;inf;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'eps'</span>),&nbsp;options.eps&nbsp;=&nbsp;0.001;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'tol'</span>),&nbsp;options.tol&nbsp;=&nbsp;0.001;&nbsp;<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;call&nbsp;MEX&nbsp;function</span><br>
<span class=comment>%---------------------------</span><br>
[model.Alpha,&nbsp;model.b,&nbsp;model.nsv,&nbsp;model.kercnt,&nbsp;model.trnerr,&nbsp;model.margin]...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;smo1d_mex(data.X,&nbsp;data.y,&nbsp;options.C,&nbsp;options.eps,&nbsp;options.tol);<br>
<br>
<span class=comment>%&nbsp;fill&nbsp;up&nbsp;the&nbsp;output&nbsp;structure</span><br>
<span class=comment>%---------------------------------</span><br>
inx&nbsp;=&nbsp;find(&nbsp;model.Alpha&nbsp;);<br>
model.sv.X&nbsp;=&nbsp;data.X(:,inx);<br>
model.sv.y&nbsp;=&nbsp;data.y(inx);<br>
model.sv.inx&nbsp;=&nbsp;inx;<br>
model.Alpha&nbsp;=&nbsp;model.Alpha(inx);<br>
model.Alpha(&nbsp;find(model.sv.y==2))&nbsp;=&nbsp;-model.Alpha(&nbsp;find(model.sv.y==2&nbsp;));<br>
model.W&nbsp;=&nbsp;model.sv.X*model.Alpha;<br>
options.ker&nbsp;=&nbsp;<span class=quotes>'linear'</span>;<br>
options.arg&nbsp;=&nbsp;1;<br>
model.options&nbsp;=&nbsp;options;<br>
model.fun&nbsp;=&nbsp;<span class=quotes>'svmclass'</span>;<br>
model.cputime&nbsp;=&nbsp;toc;<br>
<br>
<span class=jump>return</span>;<br>
<span class=comment>%&nbsp;EOF</span><br>
</code>
